package edu.ucsd.ccdb.cytoSegPopup;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.ucsd.irods.IRODSFileString;
import edu.ucsd.irods.SQLDBQuery;

public class CytosegDialog extends JDialog implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private DefaultComboBoxModel model1;
	private DefaultComboBoxModel model2;
	
	private JTextField inputTextField;
	
	private JButton runButton;
	
	private enum StateStatus { READY, EDITING_COMBOBOX };
	
	private StateStatus state;
	
	private String homeFolder ; 

	public CytosegDialog ( JFrame parent, IRODSFileString file , String homeFolder ) {
		super (parent, "Cytoseg Automatic Segmentation");
		
		this.homeFolder = homeFolder;
		
		JPanel trainingPanel = new JPanel();
		trainingPanel.setLayout(new BoxLayout(trainingPanel, BoxLayout.Y_AXIS));
		model1 = new DefaultComboBoxModel();
		model2 = new DefaultComboBoxModel();
		state = StateStatus.READY;
		
		Collection<String> data = null;
		try {
			data = SQLDBQuery.getTrainingFolderCytoSeg();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		trainingPanel.setBorder(BorderFactory.createTitledBorder("Training"));
		trainingPanel.add(new FileSelectPanel(model1, "Training folder 1: ", data));
		trainingPanel.add(Box.createRigidArea(new Dimension (0,5) ));
		trainingPanel.add(new FileSelectPanel(model2, "Training folder 2: ", null ));
	
		JPanel inputPanel = new JPanel () ;
		inputPanel.setBorder(BorderFactory.createTitledBorder("Input"));
		inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
		
		inputTextField = new JTextField(30);
		inputTextField.setText(file.getAbsolutePath());
		inputTextField.setEditable(false);
		inputPanel.add(inputTextField);
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		
		runButton = new JButton("Run");
		JButton outputButton = new JButton("Output");
		
		runButton.addActionListener(this);
		
		buttonPane.add(runButton);
		buttonPane.add(Box.createRigidArea(new Dimension (7,0) ));
		buttonPane.add(outputButton);
		
		JPanel contentPane = new JPanel();
		contentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		contentPane.add( trainingPanel );
		contentPane.add (inputPanel);
		contentPane.add (Box.createRigidArea(new Dimension(0,4) ));
		contentPane.add(buttonPane);
		
		add (contentPane);
	}
	
	public void actionPerformed(ActionEvent e) {

		runButton.setText("Thinking...");
		runButton.setEnabled(false);
		
		// run cytoseg
		CytosegTask task = new CytosegTask( (String) model1.getSelectedItem(), (String) model2.getSelectedItem() , 
									inputTextField.getText(), homeFolder, runButton );
		task.execute();
	}

	private class FileSelectPanel extends JPanel implements  ItemListener {
		
		private static final long serialVersionUID = 1L;
		private DefaultComboBoxModel model;
		
		public FileSelectPanel (DefaultComboBoxModel model, String description, Collection<String> list) {

			this.model = model;
			
			// populate the model for this list
			
			if (list != null ) {
				for ( Iterator<String> i = list.iterator(); i.hasNext();  ) {
					model.addElement(  i.next() ) ;
				}
			}
			
			JComboBox comboBox = new JComboBox(this.model);
			comboBox.addItemListener(this);
			
			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			add(new JLabel (description) );
			add(Box.createRigidArea( new Dimension( 4,0 ) ));
			add( comboBox );			
		}
		
		public void itemStateChanged(ItemEvent e) {
			
			// validation
			if (e.getStateChange() != ItemEvent.SELECTED ) return; 
			if ( state != StateStatus.READY ) return;
			
			DefaultComboBoxModel theOtherModel = null;
			
			if (model.equals(model1)) {
//				System.out.println ("Click on 1st drop down");
				theOtherModel = model2;
			}
			
			if (model.equals(model2)) {
//				System.out.println ("Click on 2nd drop down ");
				theOtherModel = model1;
			}
		
			// save theOtherModel's selection
			String savedData = (String) theOtherModel.getSelectedItem();
			
			state = StateStatus.EDITING_COMBOBOX;
			
			String data = (String) model.getSelectedItem();
			
			try {
				Set<String> list = SQLDBQuery.getTrainingFolderCytoSeg();
				
				theOtherModel.removeAllElements();
				
				for (Iterator<String> i = list.iterator(); i.hasNext(); ) {
					String string = i.next();
					if (!string.equalsIgnoreCase(data))
						theOtherModel.addElement( string );
				}
				
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			
			// put the data back
			theOtherModel.setSelectedItem(savedData);
			
			// release lock ( Why am I doing synchronization again ? ) :(
			state = StateStatus.READY;			
		}
	}
	
}
